const app = getApp();
import {getMarketDetail,marketSpeakGood,marketComment } from "../../../config/api";
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
     isSpeak:false,//是否展示评论展示框，默认不展示
     content:'',//评论的内容数据为
     detail:{},//获取的详情数据为
     last_id:null,//上个页面传递过来的id
  },

//获得内容
getContent(e){
  let that = this;
  console.log('获取的输入框内容：',e.detail.value);
  that.setData({
     content:e.detail.value
  });
},



//点击评论
toSpeak(){
  let that = this;
  console.log('点击了评论按钮！');
  that.setData({
    isSpeak:true
  });
},

// 通用的提示框
showDialogTitle(title){
  wx.showModal({
    title: '提示',
    content: title,
    showCancel:false,
    success (res) {
      if (res.confirm) {
        console.log('用户点击确定');
        return;
      } 
    }
  })
},//展示相应的弹窗框提示二次封装
// 通用的提示框

//发布评论
faBu(){
  let that = this;
  if(that.data.content.length==0){
    that.showDialogTitle('评论不能为空！')
    return;
  }
  
  let member = wx.getStorageSync('member');
  if(!member){
    that.showDialogTitle(app.globalData.info);
    return;
  }
  let params = {
    idcard:member.idcard,
    bazaar_id:that.data.last_id,
    content: that.data.content,
    user_name:member.name,
    headimg:member.heard_img?member.heard_img:''
  };
  console.log('点击了评论params！',params);
  util.apiRequest(marketComment,params).then(r=>{
    console.log('评论结果：',r.data);
    that.getDetail(that.data.last_id);
    that.setData({
      isSpeak:false
    });
  });

},


//点击点赞
toSpeakGood(){
  let that= this;
  console.log('点击了点赞按钮！');
  let member = wx.getStorageSync('member');
  //不是村民不能进行点赞，因为游客不能像皖事通那样获取身份证
  if(!member){
    that.showDialogTitle(app.globalData.info);
    return;
  }
  
  let params = {
    idcard:member.idcard,
    bazaar_id:that.data.detail.id,
    user_name:member.name,
    address:'',
  };
  util.apiRequest(marketSpeakGood,params).then(r=>{
    console.log('点赞结果为：',r.data.data);
    that.getDetail(that.data.last_id);
  });

},



//预览图片
previewImg(e){
  let that = this;
  console.log('e.currentTarget.dataset.img:',e.currentTarget.dataset.img);
  wx.previewImage(
    {
      current: e.currentTarget.dataset.img,
      urls: e.currentTarget.dataset.imgs
    }
  );
},


 //拨打电话
 dial(e){
  let that = this;
  console.log('e',e.currentTarget.dataset.phone);
  wx.makePhoneCall({
   phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
 })
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log('列表页传递过来的id为：',options.id);
      this.setData({
        last_id:options.id
      });
      this.getDetail(options.id);

  },

  getDetail(id){
    let that = this;
    let params = {
      id:id,
      idcard:wx.getStorageSync('member').idcard?wx.getStorageSync('member').idcard:null,
    };
    util.apiRequest(getMarketDetail,params,null).then(r=>{
      console.log('获取到的集市详情为：',r.data.data);
      that.setData({
        detail:r.data.data
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})